﻿using ImoviesBlazor.Client.Pages;
using ImoviesBlazor.Server.Data;
using ImoviesBlazor.Shared.Entities;

namespace ImoviesBlazor.Server.Repository
{
    public class AnimationRepository : IAnimationRepository
    {
        private readonly ApplicationDbContext _dbContext;

        public AnimationRepository(ApplicationDbContext dbContext)
        {
            this._dbContext = dbContext;
        }


        /// <summary>
        /// 获取全部数据
        /// </summary>
        /// <returns></returns>
        public Task<IEnumerable<Shared.Entities.Animation>> GetAllListAsync()
        {
            return Task.FromResult(_dbContext.Set<Shared.Entities.Animation>()
                .OrderByDescending(x => x.Year).AsEnumerable());
        }




        /// <summary>
        /// 分页获取数据
        /// </summary>
        /// <param name="pageSize"></param>
        /// <param name="pageNum"></param>
        /// <returns></returns>
        public Task<IEnumerable<Shared.Entities.Animation>> GetListAsync(int pageSize, int pageNum)
        {
            return Task.FromResult(_dbContext.Set<Shared.Entities.Animation>()
                .OrderByDescending(x => x.Year)
                .Skip((pageNum - 1) * pageSize)
                .Take(pageSize)
                .AsEnumerable());
        }


        /// <summary>
        /// 获取视频播放链接列表
        /// </summary>
        /// <param name="vid"></param>
        /// <returns></returns>
        public Task<IEnumerable<AnimationVlink>> GetVlinkList(string vid)
        {
            return Task.FromResult(_dbContext.Set<AnimationVlink>()
            .Where(x => x.AnimationId == vid)
            .AsEnumerable());
        }


        /// <summary>
        /// 查询所有年份
        /// </summary>
        /// <returns></returns>
        public Task<IEnumerable<uint>> GetAllYearsAsync()
        {
            return Task.FromResult(_dbContext.Set<Shared.Entities.Animation>()
                .Select(x => x.Year)
                .AsEnumerable());
        }


        /// <summary>
        /// 获取总记录数
        /// </summary>
        /// <returns></returns>
        public Task<int> GetTotal()
        {
            return Task.FromResult(_dbContext.Set<Shared.Entities.Animation>().Count());
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="animation"></param>
        public void AddAnimation(Shared.Entities.Animation animation)
        {
            _dbContext.Set<Shared.Entities.Animation>().Add(animation);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="animation"></param>
        public void UpdateAnimation(Shared.Entities.Animation animation)
        {
            _dbContext.Set<Shared.Entities.Animation>().Update(animation);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="animationId"></param>
        public async Task DeleteAnimation(string animationId)
        {
            var m = await _dbContext.Set<Shared.Entities.Animation>().FindAsync(animationId);
            if (m != null)
            {
                _dbContext.Set<Shared.Entities.Animation>().Remove(m);
            }
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <returns></returns>
        public Task<bool> SaveAsync()
        {
            return Task.FromResult(_dbContext.SaveChanges() > 0);
        }
    }
}
